<?php
/**
 * 测试积分计算
 */

$startTime = '2025-11-18 06:50:55';
$endTime = '2025-11-18 08:08:58';
$durationMinutes = 78;
$unitMinutes = 10;
$unitPoints = 1;

$timeSlots = [
    ['start' => '08:00', 'end' => '20:00', 'points_per_hour' => 1],
    ['start' => '05:00', 'end' => '07:59', 'points_per_hour' => 2]
];

echo "=== 测试积分计算 ===\n\n";
echo "开始时间: {$startTime}\n";
echo "结束时间: {$endTime}\n";
echo "时长: {$durationMinutes} 分钟\n";
echo "单位时长: {$unitMinutes} 分钟\n";
echo "基础积分: {$unitPoints} 分/单位\n\n";

// 解析时间段
$parsedSlots = [];
foreach ($timeSlots as $slot) {
    $slotStart = $slot['start'];
    $slotEnd = $slot['end'];
    
    list($sh, $sm) = explode(':', $slotStart);
    list($eh, $em) = explode(':', $slotEnd);
    
    $slotStartMinutes = intval($sh) * 60 + intval($sm);
    $slotEndMinutes = intval($eh) * 60 + intval($em);
    $pointsPerUnit = intval($slot['points_per_hour']);
    
    $parsedSlots[] = [
        'start' => $slotStartMinutes,
        'end' => $slotEndMinutes,
        'points_per_unit' => $pointsPerUnit,
        'label' => "{$slotStart}-{$slotEnd}"
    ];
    
    echo "时间段: {$slotStart}-{$slotEnd} ({$slotStartMinutes}-{$slotEndMinutes} 分钟) → {$pointsPerUnit} 分/单位\n";
}

// 按开始时间排序
usort($parsedSlots, function($a, $b) {
    return $a['start'] - $b['start'];
});

echo "\n=== 逐分钟计算 ===\n\n";

$startTimestamp = strtotime($startTime);
$endTimestamp = strtotime($endTime);
$currentTimestamp = $startTimestamp;

$segmentStats = [];
$minuteCount = 0;

while ($currentTimestamp < $endTimestamp) {
    $currentHour = intval(date('H', $currentTimestamp));
    $currentMinute = intval(date('i', $currentTimestamp));
    $currentTimeValue = $currentHour * 60 + $currentMinute;
    $currentTimeStr = date('H:i', $currentTimestamp);
    
    // 查找匹配的时间段
    $matchedSlot = null;
    foreach ($parsedSlots as $slot) {
        if ($currentTimeValue >= $slot['start'] && $currentTimeValue < $slot['end']) {
            $matchedSlot = $slot;
            break;
        }
    }
    
    if ($matchedSlot !== null) {
        $segmentKey = $matchedSlot['label'];
        $pointsPerUnit = $matchedSlot['points_per_unit'];
    } else {
        $segmentKey = 'base';
        $pointsPerUnit = $unitPoints;
    }
    
    if (!isset($segmentStats[$segmentKey])) {
        $segmentStats[$segmentKey] = [
            'minutes' => 0,
            'points_per_unit' => $pointsPerUnit
        ];
    }
    $segmentStats[$segmentKey]['minutes']++;
    
    $minuteCount++;
    if ($minuteCount <= 10 || $minuteCount > 68) {
        echo "{$currentTimeStr} ({$currentTimeValue}) → {$segmentKey} ({$pointsPerUnit} 分/单位)\n";
    } elseif ($minuteCount == 11) {
        echo "...\n";
    }
    
    $currentTimestamp += 60;
}

echo "\n=== 计算结果 ===\n\n";

$totalPoints = 0;
foreach ($segmentStats as $segmentKey => $stats) {
    $minutes = $stats['minutes'];
    $pointsPerUnit = $stats['points_per_unit'];
    $units = intval(floor($minutes / $unitMinutes));
    $segmentPoints = $units * $pointsPerUnit;
    $totalPoints += $segmentPoints;
    
    echo "{$segmentKey}: {$minutes} 分钟 / {$unitMinutes} = {$units} 单位 × {$pointsPerUnit} 分 = {$segmentPoints} 分\n";
}

echo "\n总积分: {$totalPoints} 分\n";

